package com.geostar.constructionland.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geostar.constructionland.index.util.FormFieldsUtils;
import com.geostar.constructionland.index.util.FormatUtils;
import com.geostar.constructionland.index.entity.*;
import com.geostar.constructionland.index.entity.model.IndexStorageFreezeModel;
import com.geostar.constructionland.index.entity.model.InsertInxStogAcotModel;
import com.geostar.constructionland.index.entity.model.InxStogAcotModel;
import com.geostar.constructionland.index.mapper.DeleteInxStorageAcotMapper;
import com.geostar.constructionland.index.mapper.IndexSupplementMapper;
import com.geostar.constructionland.index.mapper.InxOutStorageAcotMapper;
import com.geostar.constructionland.index.mapper.InxStorageAcotMapper;
import com.geostar.constructionland.index.service.InxStorageAcotService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class InxStogAcotSerImpl implements InxStorageAcotService {

    @Resource
    InxStorageAcotMapper inxStorageAcotMapper;
    @Resource
    DeleteInxStorageAcotMapper deleteInxStorageAcotMapper;
    @Resource
    IndexSupplementMapper indexSupplementMapper;
    @Resource
    InxOutStorageAcotMapper inxOutStorageAcotMapper;
    @Override
    public IPage<InxStorageAcotEntity> queryInxStogAcotList(InxStogAcotModel inxStogAcotModel) {


        Page<InxStorageAcotEntity> page = new Page<>(inxStogAcotModel.getPagenum(),inxStogAcotModel.getPagesize());
        QueryWrapper<InxStorageAcotEntity> queryWrapper=condition(inxStogAcotModel);
        page = inxStorageAcotMapper.selectPage(page,queryWrapper);
        return page;
    }

    /**
     * 查询入库台账列表数据
     * @param inxStogAcotModel
     * @return
     */
    @Override
    public List<InxStorageAcotEntity> queryInxStorageAllList(InxStogAcotModel inxStogAcotModel) {
        QueryWrapper<InxStorageAcotEntity> queryWrapper=condition(inxStogAcotModel);

        return inxStorageAcotMapper.selectList(queryWrapper);
    }

    private QueryWrapper<InxStorageAcotEntity> condition(InxStogAcotModel inxStogAcotModel) {
        QueryWrapper<InxStorageAcotEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isBlank(inxStogAcotModel.getDistCodeProvince())){
            queryWrapper.eq("xzqhdm_province",inxStogAcotModel.getDistCodeProvince());
        }
        if(!StringUtils.isBlank(inxStogAcotModel.getDistCodeCity())){
            queryWrapper.eq("xzqhdm_city",inxStogAcotModel.getDistCodeCity());
        }
        if(!StringUtils.isBlank(inxStogAcotModel.getDistCodeCountry())){
            queryWrapper.eq("xzqhdm_country",inxStogAcotModel.getDistCodeCountry());
        }
        if(!StringUtils.isBlank(inxStogAcotModel.getIndexYear())){
            queryWrapper.eq("zbnf",inxStogAcotModel.getIndexYear());
        }
        if(!StringUtils.isBlank(inxStogAcotModel.getIndexType())){
            queryWrapper.in("zblx", FormatUtils.getStringByList(inxStogAcotModel.getIndexType()));
        }

        if(!StringUtils.isBlank(inxStogAcotModel.getSortName())){
            Set<String> key1= new HashSet<>();
            // 定义修改的值(后期可做成动态)
            key1.add(inxStogAcotModel.getSortName());
            //反射获取实体类注解上配置的数据表字段名
            HashMap<String,String> conditionMap= FormFieldsUtils.getProxyPojoValue(new InxStorageAcotEntity(),key1);
            if(!StringUtils.isBlank(inxStogAcotModel.getSortOrder())){
                if (inxStogAcotModel.getSortOrder().equals("desc") ?  true:false ){
                    queryWrapper.orderByDesc(conditionMap.get(inxStogAcotModel.getSortName()));
                }else{
                    queryWrapper.orderByAsc(conditionMap.get(inxStogAcotModel.getSortName()));
                }
            }

        }
        return queryWrapper;
    }

    @Override
    public void addInxStogAcout(InxStorageAcotEntity inxStorageAcot) {
        inxStorageAcotMapper.insert(inxStorageAcot);
    }

    @Override
    public void delInxStogAcout(String id) {
//        inxStorageAcotMapper.deleteBatchIds()
    }

    @Override
    public InxStorageAcotEntity queryInxStorageAcot(String id) {
        return inxStorageAcotMapper.selectById(id);
    }

    @Override
    public void addOrUpdateInxStogAcout(InsertInxStogAcotModel insertInxStogAcotModel) {
        InxStorageAcotEntity inxStorageAcot=new InxStorageAcotEntity();
            inxStorageAcot.setDistCode(insertInxStogAcotModel.getDistCode());
            inxStorageAcot.setDistName(insertInxStogAcotModel.getDistName());
            inxStorageAcot.setIndexYear(insertInxStogAcotModel.getIndexYear());
            inxStorageAcot.setIndexName(insertInxStogAcotModel.getIndexName());
            inxStorageAcot.setIndexType(insertInxStogAcotModel.getIndexType());
            inxStorageAcot.setBuildLandAdd(insertInxStogAcotModel.getBuildLandAdd());
            inxStorageAcot.setAgclLand(insertInxStogAcotModel.getAgclLand());
            inxStorageAcot.setFarmland(insertInxStogAcotModel.getFarmland());
            inxStorageAcot.setIndexStatus(insertInxStogAcotModel.getIndexStatus());
            inxStorageAcot.setOperatorId(insertInxStogAcotModel.getOperatorId());
            inxStorageAcot.setRemark(insertInxStogAcotModel.getRemark());

            inxStorageAcot.setEffectiveTime(FormatUtils.formatStringByDate(insertInxStogAcotModel.getEffectiveTime()));
            if(!StringUtils.isBlank(insertInxStogAcotModel.getNonEffectiveTime())){
                inxStorageAcot.setNonEffectiveTime(FormatUtils.formatStringByDate(insertInxStogAcotModel.getNonEffectiveTime()));
            }
            inxStorageAcot.setDistCodeProvince(insertInxStogAcotModel.getDistCodeProvince());
            inxStorageAcot.setDistCodeCity(insertInxStogAcotModel.getDistCodeCity());
            inxStorageAcot.setDistCodeCountry(insertInxStogAcotModel.getDistCodeCountry());
            inxStorageAcot.setSpecialItemType(insertInxStogAcotModel.getSpecialItemType());
            inxStorageAcot.setOverallType(insertInxStogAcotModel.getOverallType());

        if(!StringUtils.isBlank( insertInxStogAcotModel.getId())){
            inxStorageAcot.setId(insertInxStogAcotModel.getId());
            inxStorageAcotMapper.updateById(inxStorageAcot);
        }else{
            inxStorageAcot.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            inxStorageAcot.setBusinessType("");  //ywlx 默认为空
            inxStorageAcot.setStorageTime(new Date());  //取当前时间
            inxStorageAcot.setOverallStatus("0"); // 统筹状态 默认值0，只允许新增不允许修改
            inxStorageAcotMapper.insert(inxStorageAcot);
        }

    }

    @Override
    @Transactional
    public void delInxStoragAcoutByIds(List<String> idList) {
        List<InxStorageAcotEntity> inxStorageAcots = inxStorageAcotMapper.selectBatchIds(idList);
        inxStorageAcotMapper.deleteBatchIds(idList);
        inxStorageAcots.forEach(inxStorageAcot ->{
            DeleteInxStorageAcotEntity deleteInxStorageAcot=new DeleteInxStorageAcotEntity();
            BeanUtils.copyProperties(deleteInxStorageAcot, inxStorageAcot);
            deleteInxStorageAcot.setRemoveTime(new Date());
            deleteInxStorageAcotMapper.insert(deleteInxStorageAcot);
        });


    }

    @Override
    @Transactional
    public String updateInxStorage(IndexStorageFreezeModel indexStorageFreezeModal) {
        //获取业务id，指标补录表和出库台账表的业务id需一致
        String businessId = UUID.randomUUID().toString().replaceAll("-", "");
        //获取操作时间，指标补录表和出库台账表的操作时间需一致
        Date operatorTime = new Date();

        //准备指标补录数据
        IndexSupplementEntity indexSupplement=new IndexSupplementEntity();
        //业务id
        indexSupplement.setId(businessId);
        //操作人id
        indexSupplement.setOperatorId(indexStorageFreezeModal.getOperatorId());
        //操作时间
        indexSupplement.setOperatorTime(operatorTime);
        //项目名称
        indexSupplement.setProjName(indexStorageFreezeModal.getProjectName());
        //行政区划代码
        indexSupplement.setDistCode(indexStorageFreezeModal.getDistCode());
        //行政区划名称
        indexSupplement.setDistName(indexStorageFreezeModal.getDistName());
        //行政区划代码-省
        indexSupplement.setDistCodeProvince(indexStorageFreezeModal.getDistCodeProvince());
        //行政区划代码-市
        indexSupplement.setDistCodeCity(indexStorageFreezeModal.getDistCodeCity());
        //行政区划代码-县
        indexSupplement.setDistCodeCountry(indexStorageFreezeModal.getDistCodeCountry());

        //准备指标出库台账数据
        InxOutStorageAcotEntity inxOutStorageAcot = new InxOutStorageAcotEntity();
        //业务id，和指标补录表保持一致
        inxOutStorageAcot.setBusinessId(businessId);
        //添加操作人id，和指标补录表保持一致
        inxOutStorageAcot.setOperatorId(indexStorageFreezeModal.getOperatorId());
        //添加操作时间
        inxOutStorageAcot.setOperatorTime(operatorTime);
        //入库id
        inxOutStorageAcot.setInxStorageIAcotId(indexStorageFreezeModal.getInxStorageIAcotId());
        //新增建设用地
        inxOutStorageAcot.setBuildLandAdd(indexStorageFreezeModal.getBuildLandAdd());
        //农用地
        inxOutStorageAcot.setAgclLand(indexStorageFreezeModal.getAgclLand());
        //耕地
        inxOutStorageAcot.setFarmland(indexStorageFreezeModal.getFarmland());
        //备注
        inxOutStorageAcot.setRemark(indexStorageFreezeModal.getRemark());
        //出库类型
        inxOutStorageAcot.setOutStorageType("zbbl");
        //出库状态
        inxOutStorageAcot.setOutStorageStatus("08");
        //出库时间
        inxOutStorageAcot.setOutStorageTime(new Date());
        //添加主键
        inxOutStorageAcot.setId(UUID.randomUUID().toString().replaceAll("-", ""));

        //添加指标补录数据
        indexSupplementMapper.insert(indexSupplement);
        //添加出库台账数据
        inxOutStorageAcotMapper.insert(inxOutStorageAcot);
        return "true";
    }


}
